package com.wt.admin.service.file.impl;

import cn.hutool.core.util.StrUtil;
import com.aizuda.easy.security.code.BasicCode;
import com.aizuda.easy.security.domain.Rep;
import com.aizuda.easy.security.exp.impl.BasicException;
import com.aizuda.easy.security.server.EasySecurityServer;
import com.wt.admin.config.prop.IndexProp;
import com.wt.admin.domain.vo.sys.UserVO;
import com.wt.admin.service.file.FileService;
import com.wt.admin.util.AssertUtil;
import lombok.SneakyThrows;
import org.apache.commons.io.FileUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Service
public class FileServiceImpl implements FileService {

    @Resource
    private IndexProp indexProp;
    @Resource
    private UploadFileStrategy uploadFileStrategy;
    @Resource
    private EasySecurityServer easySecurityServer;

    @Override
    public String uploadLocal(MultipartFile file,String path, HttpServletRequest httpServletRequest) throws IOException {
        String name = file.getOriginalFilename();
        String string = UUID.randomUUID().toString().replace("-", "");
        name =  string + (name.contains(".") ? name.substring(name.indexOf(".")): "");
        File fil = new File(indexProp.getFiles().getSavePath() + path + "/" + name);
        InputStream inputStream = file.getInputStream();
        FileUtils.copyInputStreamToFile(inputStream, fil);
        inputStream.close();
        return indexProp.getFiles().getSource().replace("*","") + path + "/" + name;
    }

    @Override
    public String uploadStrategy(MultipartFile file, String path, HttpServletRequest httpServletRequest) throws IOException, BasicException {
        AssertUtil.objIsNull(file, BasicCode.BASIC_CODE_99999);
        String token = httpServletRequest.getHeader("token");
        String s = uploadLocal(file, path, httpServletRequest);
        Map<String, String[]> parameterMap = httpServletRequest.getParameterMap();
        UserVO userVO = null;
        if(StrUtil.isNotEmpty(token)) {
            userVO = (UserVO) easySecurityServer.getAuthUser(token);
        }
        uploadFileStrategy.exec(s,file, parameterMap, userVO);
        return s;
    }



    @SneakyThrows
    @Override
    public List<String> uploadLocalBatch(MultipartFile[] file, String path, HttpServletRequest httpServletRequest) {
        AssertUtil.List.isEmpty(file, BasicCode.BASIC_CODE_99999);
        List<String> list = new ArrayList<>(file.length);
        for (MultipartFile multipartFile : file) {
            list.add(uploadLocal(multipartFile, path, httpServletRequest));
        }
        return list;
    }

    @SneakyThrows
    @Override
    public List<String> uploadStrategyBatch(MultipartFile[] file, String path, HttpServletRequest httpServletRequest) {
        AssertUtil.List.isEmpty(file, BasicCode.BASIC_CODE_99999);
        String token = httpServletRequest.getHeader("token");
        List<String> list = new ArrayList<>(file.length);
        for (MultipartFile multipartFile : file) {
            list.add(uploadLocal(multipartFile, path, httpServletRequest));
        }
        Map<String, String[]> parameterMap = httpServletRequest.getParameterMap();
        UserVO userVO = null;
        if(StrUtil.isNotEmpty(token)) {
            userVO = (UserVO) easySecurityServer.getAuthUser(token);
        }
        uploadFileStrategy.batchExec(list,file, parameterMap, userVO);
        return list;
    }

}
